﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class UIVKZUAB : ViewBase
    {

        public UIVKZUAB()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            try
            {
                dwZB = dwMaster;
                if (dwZB != null)
                {
                    KEY = dwZB["VKZAUFKRS"].ToString() + dwZB["VKZAUFLFD"].ToString() + dwZB["VKZAUFPOS"].ToString();
                }
                else
                {
                    KEY = "";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {

                case "ZBDEL":
                    ZBDEL(msg);
                    break;

            }
        }

        public void CREATE(Dictionary<string, object> pm)
        {
            string order = pm["ORDER"].ToString();
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["VKZMANDANT"] = app.Mandant;
            dwData["VKZSA"] = pm["VKZSA"].ToString();
            dwData["VKZAUFKRS"] = pm["KRS"].ToString();
            dwData["VKZAUFLFD"] = pm["LFD"].ToString();
            dwData["VKZAUFPOS"] = pm["POS"].ToString();
            INSERT(dwData);
        }

        public void INSERT(DataRow _dwZB)
        {
            try
            {
                TMV.InsertVKZUAB(_dwZB, app.company);
                TBMain.Rows.InsertAt(_dwZB, 0);
                vwMaster.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void ZBDEL(MessageInfo msg)
        {
            DataRow dwData = TBMain.NewRow();
            dwData["VKZMANDANT"] = app.Mandant;
            dwData["VKZSA"] = "  ";
            dwData["VKZAUFKRS"] = msg.KRS;
            dwData["VKZAUFLFD"] = msg.LFD;
            dwData["VKZAUFPOS"] = msg.POS;
            TMV.DeleteVKZUAB(dwData, 1);
        }
                

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            New();
            base.NewCommand(sender, e);
        }
        
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwZB == null)
                return;
            TMV.DeleteVKZUAB(dwZB, 1);
            base.DeleteCommand(sender, e);
        }
        int i = 1;
        public void NR_GotFocus(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TextBox control = sender as TextBox;
            if (control.Tag != null)
            {
                i = Convert.ToInt16(control.Tag);
             }
        }  
        

    }
    public partial class UIVKZUAB : ViewBase
    {
        public VVKZUAB TMV
        {
            get
            {
                return (VVKZUAB)MV;
            }
        }

        #region ==VART===========
        public static readonly DependencyProperty VARTProperty = DependencyProperty.Register("VART",
            typeof(string), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVARTChanged));
        public string VART
        {
            get
            {
                return (string)GetValue(VARTProperty);
            }
            set
            {
                SetValue(VARTProperty, value);
                OnVARTChg(value);
            }
        }
        private static void OnVARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                src.VART = (string)e.NewValue;
            }
        }
        public virtual void  OnVARTChg(string vart)
        {
        }
        #endregion
        #region ==EXCOM===========
        //用于和预定义的佣金
        public static readonly DependencyProperty EXCOMProperty = DependencyProperty.Register("EXCOM",
            typeof(string), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXCOMChanged));
        public string EXCOM
        {
            get
            {
                return (string)GetValue(EXCOMProperty);
            }
            set
            {
                SetValue(EXCOMProperty, value);
                OnEXCOMChg(value);

            }
        }
        private static void OnEXCOMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                src.EXCOM = (string)e.NewValue;
            }
        }
        public virtual void OnEXCOMChg(string com)
        {

         
        }
        #endregion

        #region ==EXBON===========
        //用于和预定义的佣金
        public static readonly DependencyProperty EXBONProperty = DependencyProperty.Register("EXBON",
            typeof(string), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXBONChanged));
        public string EXBON
        {
            get
            {
                return (string)GetValue(EXBONProperty);
            }
            set
            {
                SetValue(EXBONProperty, value);
                OnEXBONChg(value);

            }
        }
        private static void OnEXBONChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                src.EXBON = (string)e.NewValue;
            }
        }
        public virtual void OnEXBONChg(string com)
        {


        }
        #endregion

        #region ==VKZAUF===========
        public static readonly DependencyProperty VKZAUFProperty = DependencyProperty.Register("VKZAUF",
            typeof(string), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVKZAUFChanged));
        public string VKZAUF
        {
            get
            {
                return (string)GetValue(VKZAUFProperty);
            }
            set
            {
                SetValue(VKZAUFProperty, value);
            }
        }
        private static void OnVKZAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                src.VKZAUF = (string)e.NewValue;
            }
        }
        #endregion

        #region ==VKZSA===========
        public static readonly DependencyProperty VKZSAProperty = DependencyProperty.Register("VKZSA",
            typeof(string), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVKZSAChanged));
        public string VKZSA
        {
            get
            {
                return (string)GetValue(VKZSAProperty);
            }
            set
            {
                SetValue(VKZSAProperty, value);
                OnSAChg(value);
            }
        }
        private static void OnVKZSAChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                src.VKZSA = (string)e.NewValue;
            }
        }
        public virtual void OnSAChg(string sa)
        {
            if (MV == null)
                return;
            switch (sa)
            {
                default:
                case "VK":
                    TMV.VKZTYPE = "采购单";
                    break;
                case "EK":
                    TMV.VKZTYPE = "采购单";
                    break;
            }
        }

        #endregion

        #region ==VERTNR===========
        public static readonly DependencyProperty VERTNRProperty = DependencyProperty.Register("VERTNR",
            typeof(string), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVERTNRChanged));
        public string VERTNR
        {
            get
            {
                return (string)GetValue(VERTNRProperty);
            }
            set
            {
                SetValue(VERTNRProperty, value);
                OnVERTNRChg(value);

            }
        }
        private static void OnVERTNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                src.VERTNR = (string)e.NewValue;
            }
        }
        public virtual void OnVERTNRChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["VKZVERTNR" + i] = value;
            if (dwMain == null)
                return;
        }

    
        #endregion

        #region ==ZUART===========
        public static readonly DependencyProperty ZUARTProperty = DependencyProperty.Register("ZUART",
            typeof(string), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnZUARTChanged));
        public string ZUART
        {
            get
            {
                return (string)GetValue(ZUARTProperty);
            }
            set
            {
                SetValue(ZUARTProperty, value);
                OnZUARTChg(value);

            }
        }
        private static void OnZUARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                src.ZUART = (string)e.NewValue;
            }
        }
        public virtual void OnZUARTChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;

            dwMaster["VKZZUART" + i] = value.PadRight(3);
            if (dwMain == null)
                return;
        }

     
        #endregion

        #region ==RABART===========
        public static readonly DependencyProperty RABARTProperty = DependencyProperty.Register("RABART",
            typeof(string), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnRABARTChanged));
        public string RABART
        {
            get
            {
                return (string)GetValue(RABARTProperty);
            }
            set
            {
                SetValue(RABARTProperty, value);
                OnRABARTChg(value);
            }
        }
        private static void OnRABARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                src.RABART = (string)e.NewValue;
            }
        }
        public virtual void OnRABARTChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["VKZRABART" + i] = value;
            if (dwMain == null)
                return;
        }

    
        #endregion

        #region ==dwZB===========
        public static readonly DependencyProperty dwZBProperty = DependencyProperty.Register("dwZB",
            typeof(DataRow), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwZBChanged));
        public DataRow dwZB
        {
            get
            {
                return (DataRow)GetValue(dwZBProperty);
            }
            set
            {
                SetValue(dwZBProperty, value);
            }
        }
        private static void OdwZBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwZB = row;
            }
        }
        #endregion

        #region ==dwExtZB===========
        public static readonly DependencyProperty dwExtZBProperty = DependencyProperty.Register("dwExtZB",
            typeof(DataRow), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwExtZBChanged));
        public DataRow dwExtZB
        {
            get
            {
                return (DataRow)GetValue(dwExtZBProperty);
            }
            set
            {
                SetValue(dwExtZBProperty, value);
            }
        }
        private static void OdwExtZBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwExtZB = row;
            }
        }
        #endregion

        #region ==VKZSTMENGE===========
        public static readonly DependencyProperty VKZSTMENGEProperty = DependencyProperty.Register("VKZSTMENGE",
            typeof(string), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVKZSTMENGEChanged));
        public string VKZSTMENGE
        {
            get
            {
                return (string)GetValue(VKZSTMENGEProperty);
            }
            set
            {
                SetValue(VKZSTMENGEProperty, value);
            }
        }
        private static void OnVKZSTMENGEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                src.VKZSTMENGE = (string)e.NewValue;
            }
        }
        #endregion

        #region ==VKZSTPREIS===========
        public static readonly DependencyProperty VKZSTPREISProperty = DependencyProperty.Register("VKZSTPREIS",
            typeof(string), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVKZSTPREISChanged));
        public string VKZSTPREIS
        {
            get
            {
                return (string)GetValue(VKZSTPREISProperty);
            }
            set
            {
                SetValue(VKZSTPREISProperty, value);
            }
        }
        private static void OnVKZSTPREISChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                src.VKZSTPREIS = (string)e.NewValue;
            }
        }
        #endregion


        #region ==IsTake===========
        public static readonly DependencyProperty IsTakeProperty = DependencyProperty.Register("IsTake",
            typeof(bool), typeof(UIVKZUAB),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsTakeChanged));
        public bool IsTake
        {
            get
            {
                return (bool)GetValue(IsTakeProperty);
            }
            set
            {
                SetValue(IsTakeProperty, value);
                OnTakeChg(value);
            }
        }
        private static void OnIsTakeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVKZUAB;
            if (src != null)
            {
                src.IsTake = (bool)e.NewValue;
            }
        }
        public virtual void OnTakeChg(bool istake)
        { }
        #endregion
    }
}
